set more off
capture cd "C:\Users\Benjamin Allen\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "C:\Users\balle\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "~\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"

* Load data
use "data/Public+Policy+Forum+automation+and+AI+survey+April+2019_June+18,+2019_12.33 cleaned and weighted alg-sup-gov version.dta", clear

********************************************************************************
* Graphing algorithmic governance acceptibility
********************************************************************************

gen alg_govt_acceptable_1_bi_pct = alg_govt_acceptable_1_bi * 100
gen alg_govt_acceptable_2_bi_pct = alg_govt_acceptable_2_bi * 100
gen alg_govt_acceptable_3_bi_pct = alg_govt_acceptable_3_bi * 100
gen alg_govt_acceptable_4_bi_pct = alg_govt_acceptable_4_bi * 100
gen alg_govt_acceptable_5_bi_pct = alg_govt_acceptable_5_bi * 100
gen alg_govt_acceptable_6_bi_pct = alg_govt_acceptable_6_bi * 100
gen alg_govt_acceptable_7_bi_pct = alg_govt_acceptable_7_bi * 100
gen alg_govt_acceptable_8_bi_pct = alg_govt_acceptable_8_bi * 100

* Categories 
graph bar (mean) ///
	alg_govt_acceptable_1_bi_pct ///
	alg_govt_acceptable_2_bi_pct ///
	alg_govt_acceptable_3_bi_pct ///
	alg_govt_acceptable_4_bi_pct ///
	alg_govt_acceptable_5_bi_pct ///
	alg_govt_acceptable_6_bi_pct ///
	alg_govt_acceptable_7_bi_pct ///
	alg_govt_acceptable_8_bi_pct ///
	[pweight=quota_weight], ///
	///percentages ///
	title("How acceptable are the following reasons for governments to""use algorithms and AI to make decisions?", size(medsmall))  ///
	subtitle(`"Percent selected "Acceptable reason to use an algorithm""'"Canada", size(small)) ///
	ytitle("Percent") ///
	yline(50) ///
	bar(1, color(eltblue*0.7)) bar(2, color(eltblue)) bar(3, color(ebblue*0.7)) bar(4, color(ebblue)) bar(5, color(edkblue*0.7)) bar(6, color(edkblue)) bar(7, color(eltgreen)) bar(8, color(teal)) ///
	ylabel(0(10)100, angle(0) labsize(vsmall))  ///
	legend(order( /// I've put these even-then-odd, to make the result easier to read as two columns
		1 "To reduce the time required to make decisions" ///
		5 "To reduce fraud against the government." ///
		2 "To make decisions which will be a better use of""government money." ///
		6 `"To make decisions which are more consistent"'`"and less “random”."' ///
		3 "To make sure decisions are not influenced by""factors like a program recipient's gender,""ethnicity, or wealth." ///
		7 "To reduce the number of bureaucrats/government""officials." ///
		4 "To make sure decisions are not influenced by""officials’ biases." ///
		8 "To reduce the costs of government." ///
		) size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)  ///
	note("Weighted by province, age group, and gender", size(vsmall))  ///
	blabel(bar, format(%4.1f) size(medsmall))
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph export "analysis/graphs/Canada/acceptance for each reason/acceptance for each reason Canada.png", width(2000) replace

********************************************************************************
* Algorithmic governance acceptibility scale
********************************************************************************

* First, check scale features
factor alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_5_ord alg_govt_acceptable_6_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord, pcf factors(4)  mineigen(0)
* Great, the two dimensions have different loadiings on the second factor, and they split up as expected.


* Binary version
factor alg_govt_acceptable_1_bi alg_govt_acceptable_2_bi alg_govt_acceptable_3_bi alg_govt_acceptable_4_bi alg_govt_acceptable_5_bi alg_govt_acceptable_6_bi alg_govt_acceptable_7_bi alg_govt_acceptable_8_bi, pcf factors(4)  mineigen(0)


alpha alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_5_ord alg_govt_acceptable_6_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord

*sub-scales
* Efficiency
alpha alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_5_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord

* Fairness
alpha alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_6_ord



*------------------------------------------------------------------------------*
* Create scales - ordinal version
*------------------------------------------------------------------------------*

egen alg_govt_acceptable_scale_ord = rowtotal(alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_5_ord alg_govt_acceptable_6_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord)

tab alg_govt_acceptable_scale_ord [aweight = quota_weight]

* Graphing policy activism on its own
graph twoway (hist alg_govt_acceptable_scale_ord , percent bin(20) fcolor(ebblue*0.5) lcolor(ebblue*0.6)) ///
	, title("Acceptance of algorithmic governance", size(medsmall)) ///
	subtitle("Summed score across 8 reasons""Canada", size(small)) ///
	xtitle("Exposure score", size(small))  ///
	xlabel(-8(1)8, labsize(small)) ///
	ylabel(0(5)30, labsize(medsmall) angle(0)) ///
	note( ///
	"Summed score:" ///
	"-1 = Reason is unacceptable" ///
	"0 = Not sure" ///
	"1 = Reason is acceptable" ///
	, size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/Canada/number of reasons accepted/hist of alg_govt_acceptable_scale_ord.png", width(3000) replace


graph bar (percent) [aweight = quota_weight], over(alg_govt_acceptable_scale_ord) ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("Summed score across 8 reasons""Canada", size(small)) ///
	ytitle("Percent", size(small)) ///
	ylab(0(10)40, angle(0) labsize(small)) ///
	note( ///
	"Summed score:" ///
	"-1 = Reason is unacceptable" ///
	"0 = Not sure" ///
	"1 = Reason is acceptable" ///
	"Weighted by province, age group, and gender"  ///
	, size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) ///
	bar(1, lcolor(ebblue*0.9) fcolor(eltblue%80)) ///
	blabel(bar, format(%4.1f) size(small))	
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph export "analysis/graphs/Canada/number of reasons accepted/bargraph of alg_govt_acceptable_scale_ord.png", width(2000) replace

* Rescaling to 0-1
sum alg_govt_acceptable_scale_ord
replace alg_govt_acceptable_scale_ord = (alg_govt_acceptable_scale_ord -r(min)) / (r(max)-r(min))
sum alg_govt_acceptable_scale_ord
lab var alg_govt_acceptable_scale_ord "Ordered acceptance of algorithmic governance (scaled 0-1)"

*---------------------------------------*
* Sub-scales
*---------------------------------------*

* Efficiency
egen alg_govt_acc_eff_scale_ord = rowtotal( alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_5_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord)

* Fairness
egen alg_govt_acc_fair_scale_ord = rowtotal( alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_6_ord) 

graph twoway ///
	(scatter alg_govt_acc_fair_scale_ord alg_govt_acc_eff_scale_ord [aweight = quota_weight], jitter(6) mcolor(ebblue%25) msize(tiny)) ///
	(lfit alg_govt_acc_fair_scale_ord alg_govt_acc_eff_scale_ord [aweight = quota_weight]) ///
	, ///
	title("Average score of reasons", size(medsmall)) ///
	subtitle("Comparing dimensions""Canada", size(small)) ///
	ytitle("Fairness") ///
	xtitle("Efficiency") ///
	ylabel(-3(1)3) ///
	xlabel(-5(1)5) ///
	legend(off) ///
	note("Weighted by province, age group, and gender", size(vsmall))  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/Canada/scatterplots/scatterplot of alg_govt_acc_fair_scale_ord and alg_govt_acc_eff_scale_ord.png", width(3000) replace

* Rescaling to 0-1

* Efficiency
sum alg_govt_acc_eff_scale_ord
replace alg_govt_acc_eff_scale_ord = (alg_govt_acc_eff_scale_ord -r(min)) / (r(max)-r(min))
sum alg_govt_acc_eff_scale_ord
lab var alg_govt_acc_eff_scale_ord "Ordered acceptance of algorithmic governance efficiency items (scaled 0-1)"

* Fairness
sum alg_govt_acc_fair_scale_ord
replace alg_govt_acc_fair_scale_ord = (alg_govt_acc_fair_scale_ord -r(min)) / (r(max)-r(min))
sum alg_govt_acc_fair_scale_ord
lab var alg_govt_acc_fair_scale_ord "Ordered acceptance of algorithmic governance fairness items (scaled 0-1)"

*------------------------------------------------------------------------------*
* Create scales - binary version
*------------------------------------------------------------------------------*

egen alg_govt_acceptable_scale_bi = rowtotal(alg_govt_acceptable_1_bi alg_govt_acceptable_2_bi alg_govt_acceptable_3_bi alg_govt_acceptable_4_bi alg_govt_acceptable_5_bi alg_govt_acceptable_6_bi alg_govt_acceptable_7_bi alg_govt_acceptable_8_bi)

tab alg_govt_acceptable_scale_bi [aweight = quota_weight]

* 67.04% find at least half of the reasons accetpable
* 23.85% find all of the reasons acceptable

/* OLD WAY OF GRAPHING IT - the bars don't look quite right this way, and we can't label them

* Graphing policy activism on its own
graph twoway (hist alg_govt_acceptable_scale_bi , percent  fcolor(ebblue*0.5) lcolor(ebblue*0.6)) ///
	, title("Acceptance of algorithmic governance", size(medsmall)) ///
	subtitle("Total accepted among 8 reasons", size(small)) ///
	xtitle("Number of reasons found acceptable", size(small))  ///
	xlabel(0(1)8, labsize(small)) ///
	ylabel(0(5)30, labsize(medsmall) angle(0)) ///
	note( ///
	"Average score of reasons:" ///
	"0 = Unacceptable or Not sure" ///
	"1 = Reason is acceptable" ///
	, size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/hist of alg_govt_acceptable_scale_bi.png", width(3000) replace

*/

graph bar (percent) [aweight = quota_weight], over(alg_govt_acceptable_scale_bi) ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("Number of reasons found acceptable""Canada", size(small)) ///
	ytitle("Percent", size(small)) ///
	ylab(0(10)40, angle(0) labsize(small)) ///
	note("Weighted by province, age group, and gender", size(vsmall))  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) ///
	bar(1, lcolor(ebblue*0.9) fcolor(eltblue%80)) ///
	blabel(bar, format(%4.1f) size(medsmall))	
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph export "analysis/graphs/Canada/number of reasons accepted/bargraph of alg_govt_acceptable_scale_bi.png", width(2000) replace

* Fairness
sum alg_govt_acceptable_scale_bi
replace alg_govt_acceptable_scale_bi = (alg_govt_acceptable_scale_bi -r(min)) / (r(max)-r(min))
sum alg_govt_acceptable_scale_bi
lab var alg_govt_acceptable_scale_bi "Binary acceptance of algorithmic governance (scaled 0-1)"

*---------------------------------------*
* Sub-scales
*---------------------------------------*

* Efficiency
egen alg_govt_acc_eff_scale_bi = rowtotal( alg_govt_acceptable_1_bi alg_govt_acceptable_2_bi alg_govt_acceptable_5_bi alg_govt_acceptable_7_bi alg_govt_acceptable_8_bi)

* Fairness
egen alg_govt_acc_fair_scale_bi = rowtotal( alg_govt_acceptable_3_bi alg_govt_acceptable_4_bi alg_govt_acceptable_6_bi) 

tab alg_govt_acc_fair_scale_bi alg_govt_acc_eff_scale_bi [aweight = quota_weight], cell

graph twoway ///
	(scatter alg_govt_acc_fair_scale_bi alg_govt_acc_eff_scale_bi [aweight = quota_weight], jitter(6) mcolor(ebblue%25) msize(tiny)) ///
	(lfit alg_govt_acc_fair_scale_bi alg_govt_acc_eff_scale_bi [aweight = quota_weight]) ///
	, ///
	title("Number of reasons accepted", size(medsmall)) ///
	subtitle("Comparing dimensions""Canada", size(small)) ///
	ytitle("Fairness") ///
	xtitle("Efficiency") ///
	ylabel(0(1)3) ///
	xlabel(0(1)5) ///
	legend(off) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/Canada/scatterplots/scatterplot of alg_govt_acc_fair_scale_bi and alg_govt_acc_eff_scale_bi.png", width(3000) replace

	
* Rescaling to 0-1

* Efficiency
sum alg_govt_acc_eff_scale_bi
replace alg_govt_acc_eff_scale_bi = (alg_govt_acc_eff_scale_bi -r(min)) / (r(max)-r(min))
lab var alg_govt_acc_eff_scale_bi "Binary acceptance of algorithmic governance efficiency items (scaled 0-1)"
sum alg_govt_acc_eff_scale_bi

* Fairness
sum alg_govt_acc_fair_scale_bi
replace alg_govt_acc_fair_scale_bi = (alg_govt_acc_fair_scale_bi -r(min)) / (r(max)-r(min))
sum alg_govt_acc_fair_scale_bi
lab var alg_govt_acc_fair_scale_bi "Binary acceptance of algorithmic governance fairness items (scaled 0-1)"


/* ALTERNATE WAY OF GRAPHING IT
preserve

collapse (count) N = gc, by(cps19_surveyday)

graph twoway (bar N cps19_surveyday , lcolor(ebblue*0.9) fcolor(eltblue%80)) ///
	, ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("CES 2019 Campaign Period Survey", size(small)) ///
	ytitle("Number of responses", size(small)) ///
	xtitle("Survey day", size(small)) ///
	xlab(1(2)39, valuelabel angle(45) labsize(small)) ///
	ylab(0(250)2000, angle(0) labsize(small)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)

restore
*/

********************************************************************************
* Algorithmic governance acceptibility subscale categories
********************************************************************************

* Efficiency
tab alg_govt_acc_eff_scale_bi

gen alg_gov_sup_eff = 0
replace alg_gov_sup_eff = 1 if alg_govt_acc_eff_scale_bi >= 3

lab var alg_gov_sup_eff "Acceptance for a majority of efficiency reasons"
lab def alg_gov_sup_eff 0 "Does not accept majority" 1 "Accepts majority"
lab val alg_gov_sup_eff alg_gov_sup_eff

* Fairness
tab alg_govt_acc_fair_scale_bi

gen alg_gov_sup_fair = 0
replace alg_gov_sup_fair = 1 if alg_govt_acc_fair_scale_bi >= 2

lab var alg_gov_sup_fair "Support for a majority of fairness reasons"
lab def alg_gov_sup_fair 0 "Does not accept majority" 1 "Accepts majority"
lab val alg_gov_sup_fair alg_gov_sup_fair

* Twoway table
tab alg_gov_sup_eff alg_gov_sup_fair, cell

********************************************************************************
* Creating anti-politics scale
********************************************************************************

*------------------------------------------------------------------------------*
* Populism
*------------------------------------------------------------------------------*

* Creating combined "populism" scale, and anti-elitism and strong leader subscales

codebook populism_1 populism_2 populism_3 populism_4 populism_5 populism_6 populism_7

* Raw populism. Only the last item (populism_7) is reversed
gen POP1 = (populism_1 -1)/4
gen POP2 = (populism_2 -1)/4
gen POP3 = (populism_3 -1)/4
gen POP4 = (populism_4 -1)/4
gen POP5 = (populism_5 -1)/4
gen POP6 = (populism_6 -1)/4
gen POP7 = (populism_7*-1 +5)/4

sum POP1-POP7

egen populism_scale = rowmean(POP1-POP7)
lab var populism_scale "Populism (0-1)"

sum populism_scale

alpha POP1-POP7
* 0.71 isn't great. Does removing the reversed gov't trust Q help?
alpha POP1-POP6
* 0.74, so a little

factor POP1-POP7


* Anti-elitism subscale
egen antielitism = rowmean(POP1 POP2 POP3 POP7)
lab var antielitism "Anti-elitism (0-1)"

sum antielitism

sum POP1 POP2 POP3 POP7
alpha POP1 POP2 POP3 POP7
factor POP1 POP2 POP3 POP7


* Strong leader subscale
egen strongleader = rowmean(POP4 POP5 POP6)
lab var strongleader "Strong Leader (0-1)"

sum strongleader

sum POP4 POP5 POP6
alpha POP4 POP5 POP6
factor POP4 POP5 POP6

********************************************************************************
* Creating nativism scale
********************************************************************************

codebook nativism_1 nativism_2 nativism_3 nativism_4 nativism_5 nativism_6

* Only the 4th Nativism item is reversed
gen NAT1 = (nativism_1 -1)/4
gen NAT2 = (nativism_2 -1)/4
gen NAT3 = (nativism_3 -1)/4
gen NAT4 = (nativism_4*-1 +5)/4
gen NAT5 = (nativism_5 -1)/4
gen NAT6 = (nativism_6 -1)/4

sum NAT1-NAT6

egen nativism_scale = rowmean(NAT1-NAT6)
lab var nativism_scale "Nativism (0-1)"

sum nativism_scale

alpha NAT1-NAT6
* Wow, that alpha is so much stronger

factor NAT1-NAT6

********************************************************************************
* Rescaling other variables to 0-1
********************************************************************************

sum age_in_years
gen age_in_years_rescaled = (age_in_years -r(min)) / (r(max)-r(min))
sum age_in_years_rescaled
lab var age_in_years_rescaled "Age in years (rescaled 0-1)"

sum income
gen income_rescaled = (income -r(min)) / (r(max)-r(min))
sum income_rescaled
lab var income_rescaled "Income category (rescaled 0-1)"

sum education
gen education_rescaled = (education -r(min)) / (r(max)-r(min))
sum education_rescaled
lab var education_rescaled "Education category (rescaled 0-1)"

sum lr_scale
gen lr_scale_rescaled = (lr_scale -r(min)) / (r(max)-r(min))
sum lr_scale_rescaled
lab var lr_scale_rescaled "Left-Right scale (rescaled 0-1)"

/*

*standardizing the variable to 0-1
sum combined_jobloss_scale
replace combined_jobloss_scale = (combined_jobloss_scale-r(min))/ (r(max)-r(min))
sum combined_jobloss_scale

lab var combined_jobloss_scale "Personal and prosocial job loss expectations (-8/+8)"

*/

save "data/Public+Policy+Forum+automation+and+AI+survey+April+2019_June+18,+2019_12.33 cleaned and weighted alg-sup-gov version with scales.dta", replace
